package com.duowei.dw_pos.dialog;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatDialogFragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.duowei.dw_pos.R;
import com.duowei.dw_pos.adapter.SpacesItemDecoration;
import com.duowei.dw_pos.bean.LBZKSZJBXX;
import com.duowei.dw_pos.bean.LBZKSZMXXX;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class KindDiscountFragment extends AppCompatDialogFragment {

    private List<LBZKSZJBXX> mLbzkszjbxxList;

    public KindDiscountFragment() {
        // Required empty public constructor
    }

    public interface DiscountClick{
        void onDiscountClick(List<LBZKSZMXXX> lbzkszmxxxList,String mc);
    }
    private DiscountClick mDiscountClick;
    public void setOnDiscountClick(DiscountClick click){
        mDiscountClick=click;
    }

    public static KindDiscountFragment newInstance(List<LBZKSZJBXX>lbzkszjbxxList) {
        Bundle args = new Bundle();
        args.putParcelableArrayList("list", (ArrayList<? extends Parcelable>) lbzkszjbxxList);
        KindDiscountFragment fragment = new KindDiscountFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_kind_discount, container, false);
        mLbzkszjbxxList = getArguments().getParcelableArrayList("list");
        RecyclerView rv = (RecyclerView) inflate.findViewById(R.id.rv);
        rv.setLayoutManager(new GridLayoutManager(getActivity(),3));
        rv.addItemDecoration(new SpacesItemDecoration(5));
        rv.setAdapter(new RvAdapter());
        return inflate;
    }
    class RvAdapter extends RecyclerView.Adapter<RvAdapter.ViewHold>{
        @Override
        public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.customer_item, null);
            ViewHold viewHold = new ViewHold(inflate);
            return viewHold;
        }

        @Override
        public void onBindViewHolder(ViewHold holder, int position) {
            final LBZKSZJBXX lbzkszjbxx = mLbzkszjbxxList.get(position);
            holder.mButton.setText(lbzkszjbxx.getMC());
            holder.mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    List<LBZKSZMXXX> lbzkszmxxxList = DataSupport.where("bm=?", lbzkszjbxx.getBM()).find(LBZKSZMXXX.class);
                    mDiscountClick.onDiscountClick(lbzkszmxxxList,lbzkszjbxx.getMC());
                    dismiss();
                }
            });
        }

        @Override
        public int getItemCount() {
            return mLbzkszjbxxList.size();
        }

        class ViewHold extends RecyclerView.ViewHolder{
            Button mButton;
            public ViewHold(View itemView) {
                super(itemView);
                mButton=(Button)itemView.findViewById(R.id.btn_customer);
            }
        }
    }
}
